package generadorDescriptores.forms.edition;

import generadorDescriptores.Fachada;
import generadorDescriptores.FileInEdition;

import javax.swing.JOptionPane;

import com.project.frba.bi.descriptor.DatabaseDescriptor;

public class FormEditarNombre extends javax.swing.JDialog {

	Fachada fachada = Fachada.getInstance();

	/** Creates new form FormEditarNombre */
	public FormEditarNombre(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		this.setLocationRelativeTo(null);
		this.setTitle("Editar Nombre del Descriptor");
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.loadData();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		TextFieldNombreDescriptor = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		TextFieldDescripcion = new javax.swing.JTextField();
		ButtonAceptar = new javax.swing.JButton();
		ButtonCancelar = new javax.swing.JButton();
		jLabel3 = new javax.swing.JLabel();
		ComboBoxPrivilege = new javax.swing.JComboBox();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jLabel1.setText("Nombre del Descriptor:");

		jLabel2.setText("Descripci\u00f3n del Descriptor:");

		ButtonAceptar.setText("Aceptar");
		ButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ButtonAceptarActionPerformed(evt);
			}
		});

		ButtonCancelar.setText("Cancelar");
		ButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ButtonCancelarActionPerformed(evt);
			}
		});

		jLabel3.setText("Privilegio:");

		ComboBoxPrivilege.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "general", "restringido" }));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				jLabel2)
																		.addContainerGap(
																				119,
																				Short.MAX_VALUE))
														.add(jLabel1)
														.add(
																org.jdesktop.layout.GroupLayout.TRAILING,
																layout
																		.createSequentialGroup()
																		.add(
																				ButtonAceptar)
																		.add(
																				18,
																				18,
																				18)
																		.add(
																				ButtonCancelar)
																		.add(
																				51,
																				51,
																				51))
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				jLabel3)
																		.addContainerGap(
																				200,
																				Short.MAX_VALUE))
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				layout
																						.createParallelGroup(
																								org.jdesktop.layout.GroupLayout.TRAILING,
																								false)
																						.add(
																								org.jdesktop.layout.GroupLayout.LEADING,
																								ComboBoxPrivilege,
																								0,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.add(
																								org.jdesktop.layout.GroupLayout.LEADING,
																								TextFieldDescripcion)
																						.add(
																								org.jdesktop.layout.GroupLayout.LEADING,
																								TextFieldNombreDescriptor,
																								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																								210,
																								Short.MAX_VALUE))
																		.addContainerGap()))));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(jLabel1)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												TextFieldNombreDescriptor,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(41, 41, 41)
										.add(jLabel2)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												TextFieldDescripcion,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(26, 26, 26)
										.add(jLabel3)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												ComboBoxPrivilege,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED,
												36, Short.MAX_VALUE)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(ButtonAceptar)
														.add(ButtonCancelar))
										.addContainerGap()));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void ButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {
		this.setVisible(false);
	}

	private void ButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {
		if (!isAnyoneEmpty()) {
			FileInEdition file = fachada.getFileInEditionByIndex(fachada.getUI().getActualTabIndex());
			DatabaseDescriptor descriptor = file.getDescriptor();
			descriptor.setDescriptorName(TextFieldNombreDescriptor.getText());
			descriptor.setLabel(TextFieldDescripcion.getText());
			descriptor.setPrivilege(ComboBoxPrivilege.getSelectedItem().toString());
			file.refreshDocument();
			fachada.getUI().refreshFileInEdition(file.getXmlDocument());
			this.setVisible(false);
		}
	}

	private void loadData() {
		int index = fachada.getUI().getActualTabIndex();
		if (index != -1) {
			FileInEdition file = fachada.getFileInEditionByIndex(index);
			TextFieldNombreDescriptor.setText(file.getDescriptor()
					.getDescriptorName());
			TextFieldDescripcion.setText(file.getDescriptor().getLabel());
			ComboBoxPrivilege.setSelectedItem(file.getDescriptor().getPrivilege());
		}
	}

	private boolean isAnyoneEmpty() {
		if (this.TextFieldNombreDescriptor.getText().equals("")
				|| this.TextFieldDescripcion.getText().equals("")) {
			JOptionPane.showMessageDialog(null,
					"Debe completar todos los datos solicitados",
					"Error en el ingreso de datos", JOptionPane.ERROR_MESSAGE);
			return true;
		} else
			return false;
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				FormEditarNombre formEditarNombre = new FormEditarNombre(new javax.swing.JFrame(), true);
				formEditarNombre.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {

					}
				});
				formEditarNombre.setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton ButtonAceptar;
	private javax.swing.JButton ButtonCancelar;
	private javax.swing.JComboBox ComboBoxPrivilege;
	private javax.swing.JTextField TextFieldDescripcion;
	private javax.swing.JTextField TextFieldNombreDescriptor;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	// End of variables declaration//GEN-END:variables

}